/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = jolt_header.c                                      */
/*  NOTE      =                                                    */
/*  DATE      = 2015/06/02 by songbb                               */
/*******************************************************************/

#include <stdio.h>
#include <netinet/in.h>
#include <frct_comm.h>

#include "jolt_comm.h"
#include "jolt_core.h"

#define APM_JOLT_HEADER_MAGIC 0x4a4f4c54

/*******************************************************************************
*  FUNC     :  检测通用包头
*           :  *p_payload                           (数据内容)
*           :  ui_payload_len                       (数据长度)
*  RTN      :  APM_L7_IS_YES                        (是Jolt协议)
*              APM_L7_IS_UNKNOWN                    (不能判断该协议)
*  NOTE     :
*******************************************************************************/
int jolt_header_detect(char *p_payload
                    , unsigned int ui_payload_len)
{
    unsigned int *p_ui_magic;
    unsigned int ui_magic;

    if (ui_payload_len < APM_JOLT_PKT_HEADER_LEN)
        goto unknown;

    p_ui_magic = (unsigned int *) p_payload;
    ui_magic = (unsigned int) ntohl(*p_ui_magic);

    if (ui_magic != APM_JOLT_HEADER_MAGIC)
        goto unknown;

    return APM_L7_IS_YES;
unknown:
    return APM_L7_IS_UNKNOWN;
}

/*******************************************************************************
*  FUNC     :  解析通用包头
*  ARGS     :  *p_payload                           (数据内容)
*           :  ui_payload_len                       (数据长度)
*           :  *p_st_jolt_header                     (通用头结构体)
*  RTN      :  0                                    (FR_SUCC)
*              1                                    (FR_FAIL)
*  NOTE     :
*******************************************************************************/
int jolt_header_unpack(char *p_payload
                    , unsigned int ui_payload_len
                    , Jolt_header *p_st_jolt_header)
{
    unsigned int *p_ui_magic;
    unsigned int ui_magic;
    unsigned int *p_ui_version;
    unsigned int *p_ui_length;
    unsigned int *p_ui_msgcode;
    unsigned int *p_ui_session_id;
    unsigned int *p_ui_handler_id;
    unsigned int *p_ui_msg_id;
    unsigned int *p_ui_uncmprsz;

    if (ui_payload_len < APM_JOLT_PKT_HEADER_LEN)
        goto error;

    p_ui_magic = (unsigned int *) p_payload;
    ui_magic = (unsigned int) ntohl(*p_ui_magic);

    if (ui_magic != APM_JOLT_HEADER_MAGIC)
        goto error;

    p_ui_version = (unsigned int *)(p_payload + 4);
    p_st_jolt_header->ui_version = (unsigned int) ntohl(*p_ui_version);

    p_ui_length = (unsigned int *)(p_payload + 8);
    p_st_jolt_header->ui_length = (unsigned int) ntohl(*p_ui_length);

    p_ui_msgcode = (unsigned int *)(p_payload + 16);
    p_st_jolt_header->ui_msgcode = (unsigned int) ntohl(*p_ui_msgcode);

    p_ui_session_id = (unsigned int *)(p_payload + 24);
    p_st_jolt_header->ui_session_id = (unsigned int) ntohl(*p_ui_session_id);

    p_ui_handler_id = (unsigned int *)(p_payload + 28);
    p_st_jolt_header->ui_handler_id = (unsigned int) ntohl(*p_ui_handler_id);

    p_ui_msg_id = (unsigned int *)(p_payload + 32);
    p_st_jolt_header->ui_msg_id = (unsigned int) ntohl(*p_ui_msg_id);

    p_ui_uncmprsz = (unsigned int *)(p_payload + 36);
    p_st_jolt_header->ui_uncmprsz = (unsigned int) ntohl(*p_ui_uncmprsz);

    return FR_SUCC;
error:
    return FR_FAIL;
}
